#include "Object2DTranformation.h"


// Object2DTranformation methods



Object2DTransformation::Object2DTransformation(){

   
} 


Object2DTransformation::Object2DTransformation(long numRows,long numCols,double val):
                                        Matrix(numRows,numCols,val){

   //this->disp();
}


Object2DTransformation::Object2DTransformation(const char *fileName):Matrix(fileName){

   //transformedBy = new Matrix (fileName);
   //transformedBy(fileName);
   
  //   transformedBy->disp();
     //this->disp(); 
   cout<<"Object is taken from "<<fileName<<endl;
}


Object2DTransformation::Object2DTransformation(const Object2DTransformation & another){

       (*this) = another;
     
}



Object2DTransformation::~Object2DTransformation(){ 

}


//void Object2DTransformation :: setTransformedByMatrix(const Matrix &T){

       //transformedBy = new Matrix(T);
       //transformedBy->disp();
//}


bool Object2DTransformation::transform(const Matrix & T){

        
	transformedObject = new Matrix(numRows(), T.numCols(),1);
	      
        (*transformedObject) = (*this) * T; 
      
     //   transformedObject->disp();
      
            
      
      return 1;               
}


Matrix Object2DTransformation :: transform(const double dx, const double dy){

        Matrix T(3,3,0);
	
	T[0][0] = 1;
	T[1][1] = 1;
	T[2][2] = 1;
	
	
	T[3][0] = dx;
	T[3][1] = dy;
	  
	transform(T);
	
     return (*transformedObject);        
}


bool Object2DTransformation::rotate(const double angle){
   
    Matrix T(3,3);
    
    T[0][0] =abs(cos( abs(angle) ));
    T[0][1] =sin( abs(angle) );
    T[1][0] =sin(- abs(angle));
    T[1][1] =abs( cos( abs(angle) ));
    T[2][2] = 1;
    
    
      //T.save("RotationMatrix.txt");
       transformedObject = new Matrix(numRows(),T.numCols());
       
     (*transformedObject) = (*this) * T; 
      
     //transformedObject->disp();
      
    
  return 1;
}

bool Object2DTransformation::scale(const Matrix & A){

   transformedObject = new Matrix(numRows(),A.numCols());

        (*transformedObject) = (*this) * A; 
	
    //transformedObject->disp();
    	
   return 1;
}





bool Object2DTransformation::scale(const double dx , const double dy){

    Matrix T(3,3,0);
    
    
    T[0][0] = dx;
    T[1][1] = dy;
    T[2][2] = 1;
    
    //T.save("ScalingMatrix.txt");
              
    scale(T);
      
     
      
    
  return 1;
    
}

bool Object2DTransformation::sheare(const Matrix & A){


    transformedObject = new Matrix(numRows(),A.numCols());
       
     (*transformedObject) = (*this) * A; 
      
     //transformedObject->disp();

  return 1;
}




bool Object2DTransformation::sheare(const double dx ,const double dy){
   
    Matrix T(3,3,0);
    
    
    T[0][1] = dy;
    T[0][0] = 1;
    
    T[1][0] = dx;
    T[1][1] = 1;
    
    T[2][2] = 1;
  
      sheare(T); 
    //T.save("ShearingMatrix.txt");
    
          
  
  return 1;
}



bool Object2DTransformation::save(const char *fileName){

       ofstream out(fileName);
	
	if(!out)
	{
	    cout <<"\n unable to open file : " <<fileName;
		return false;
	}
	
	out <<numRows() <<"   "  <<numCols() <<"\n\n";
	
	long i,j;
	for(i=0;i<numRows();i++)
	{
	    for(j=0;j<numCols();j++)
		{
		    out <<(*transformedObject)[i][j] <<"   ";
		}
		
		out <<"\n";
	}
	
	out <<"\n";
	
	return true;


}



Matrix Object2DTransformation::getTransformedObject(){

  return (*transformedObject);
}



